'use strict';

var _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');
var _interopRequireWildcard = require('@babel/runtime/helpers/interopRequireWildcard');
Object.defineProperty(exports, '__esModule', {
  value: true,
});
exports.default = exports.Field = void 0;
var _objectSpread2 = _interopRequireDefault(require('@babel/runtime/helpers/objectSpread2'));
var _objectWithoutProperties2 = _interopRequireDefault(
  require('@babel/runtime/helpers/objectWithoutProperties'),
);
var _react = _interopRequireWildcard(require('react'));
var _omit = _interopRequireDefault(require('omit.js'));
var _FormItem = _interopRequireDefault(require('../FormItem'));
var _FormContext = require('../contexts/FormContext');
var _FormAttrsContext = require('../contexts/FormAttrsContext');
var _detail = require('../detail');
var _utils = require('./utils');
var _utils2 = require('../utils');
var _consts = require('./consts');
var _excluded = ['children', 'detail', 'detailType', 'DetailComponent'],
  _excluded2 = ['formItemClassName', 'formItemStyle'];
/**
 * 创建 Form.Item 包裹的表单控件，提供详情展示
 * @param FieldComponent
 * @param options
 * @returns
 */
function createField(FieldComponent, options) {
  var Field = function Field(_ref) {
    var children = _ref.children,
      detailProp = _ref.detail,
      detailTypeProp = _ref.detailType,
      DetailComponentProp = _ref.DetailComponent,
      rest = (0, _objectWithoutProperties2.default)(_ref, _excluded);
    var form = (0, _FormContext.useFormContext)();
    var _useFormAttrsContext = (0, _FormAttrsContext.useFormAttrsContext)(),
      detail = _useFormAttrsContext.detail;
    var isDetail = (0, _react.useMemo)(
      function () {
        return detailProp !== undefined ? detailProp : detail;
      },
      [detail, detailProp],
    );
    var _ref2 = options || {},
      _ref2$defaultDetailTy = _ref2.defaultDetailType,
      defaultDetailType = _ref2$defaultDetailTy === void 0 ? 'text' : _ref2$defaultDetailTy,
      defaultDetailComponent = _ref2.defaultDetailComponent,
      _ref2$defaultFormItem = _ref2.defaultFormItemProps,
      defaultFormItemProps = _ref2$defaultFormItem === void 0 ? {} : _ref2$defaultFormItem,
      getFormItemPropsFromProps = _ref2.getFormItemPropsFromProps;
    var basicFormItemProps = (0, _utils2.extract)(
      rest,
      isDetail ? _consts.DetailFormItemPropNames : _consts.AntdFormItemPropNames,
    );
    var extraFormItemProps = getFormItemPropsFromProps
      ? getFormItemPropsFromProps(rest, form)
      : undefined;
    var formItemProps = (0, _objectSpread2.default)(
      (0, _objectSpread2.default)(
        (0, _objectSpread2.default)({}, defaultFormItemProps),
        basicFormItemProps,
      ),
      extraFormItemProps,
    );
    var formItemClassName = formItemProps.formItemClassName,
      formItemStyle = formItemProps.formItemStyle,
      formItemRest = (0, _objectWithoutProperties2.default)(formItemProps, _excluded2);
    // @ts-ignore
    var fieldProps = (0, _omit.default)(rest, _consts.AntdFormItemPropNames);
    var DetailComponent;
    if (DetailComponentProp) {
      DetailComponent = DetailComponentProp;
    } else if (detailTypeProp) {
      DetailComponent = _detail.DetailComponents[detailTypeProp];
    } else if (defaultDetailComponent) {
      DetailComponent = defaultDetailComponent;
    } else {
      DetailComponent = _detail.DetailComponents[defaultDetailType];
    }
    if (isDetail) {
      return /*#__PURE__*/ _react.default.createElement(
        _FormItem.default,
        (0, _objectSpread2.default)(
          {
            className: formItemClassName,
            style: formItemStyle,
          },
          formItemRest,
        ),
        /*#__PURE__*/ _react.default.createElement(
          DetailComponent,
          (0, _objectSpread2.default)(
            (0, _objectSpread2.default)({}, fieldProps),
            {},
            {
              valuePropName: formItemProps.valuePropName,
            },
          ),
          children,
        ),
      );
    }
    return /*#__PURE__*/ _react.default.createElement(
      _FormItem.default,
      (0, _objectSpread2.default)(
        (0, _objectSpread2.default)(
          {
            className: formItemClassName,
            style: formItemStyle,
          },
          formItemRest,
        ),
        {},
        {
          rules: (0, _utils.getRealRules)(formItemRest, form),
        },
      ),
      /*#__PURE__*/ _react.default.createElement(
        FieldComponent,
        (0, _objectSpread2.default)(
          {},
          (0, _utils.enhanceFieldProps)(fieldProps, formItemRest.rules, form),
        ),
        children,
      ),
    );
  };
  return Field;
}
function FieldDecorator(options) {
  if (!options) {
    return function (FieldComponent) {
      return createField(FieldComponent, options);
    };
  }
  return function (FieldComponent) {
    return createField(FieldComponent, options);
  };
}
var _default = createField;
exports.default = _default;
var Field = FieldDecorator;
exports.Field = Field;
